Page({
  data: {
    year: 2025,
    month: 3,
    weekDays: ['日', '一', '二', '三', '四', '五', '六'],
    days: [],
    tab: 'weekly',
    selectedDate: '',
    today: '',
    type: ''
  },

  onLoad(options) {
    const now = new Date()
    const today = now.toISOString().split('T')[0]
    const selectedDate = today
    
    this.setData({
      type: options.type || '',
      today,
      selectedDate,
      year: now.getFullYear(),
      month: now.getMonth() + 1
    })
    this.generateCalendar(selectedDate)
  },

  generateCalendar(selectedDate) {
    const date = new Date(selectedDate)
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const days = []
    const firstDay = new Date(year, month - 1, 1)
    const lastDay = new Date(year, month, 0)
    
    // 填充上个月的日期
    const firstDayWeek = firstDay.getDay()
    for (let i = firstDayWeek - 1; i >= 0; i--) {
      const date = new Date(year, month - 1, -i)
      days.push({
        day: date.getDate(),
        month: date.getMonth() + 1,
        date: this.formatDate(date),
        isCurrentMonth: false,
        isToday: false,
        disabled: true,
        hasData: false
      })
    }

    // 填充当月日期
    const maxDate = new Date()
    maxDate.setHours(23, 59, 59, 999)
    const today = this.formatDate(new Date())
    
    for (let i = 1; i <= lastDay.getDate(); i++) {
      const date = new Date(year, month - 1, i)
      const dateStr = this.formatDate(date)
      days.push({
        day: i,
        month,
        date: dateStr,
        isCurrentMonth: true,
        isToday: dateStr === today,
        isSelected: dateStr === this.data.selectedDate,
        disabled: date > maxDate,
        hasData: Math.random() > 0.5 // 模拟数据，实际应该根据真实数据判断
      })
    }

    this.setData({ days })
  },

  onDayTap(e) {
    const { date, disabled } = e.currentTarget.dataset
    if (disabled) return
    
    // 返回到上一页并传递选中的日期
    const pages = getCurrentPages()
    const prevPage = pages[pages.length - 2]
    
    // 更新上一页的数据
    prevPage.setData({
      selectedDateStr: date,
      currentDate: this.formatDateToString(new Date(date)),
      selectedDate: new Date(date)
    }, () => {
      // 确保数据设置完成后再返回
      wx.navigateBack()
    })
  },

  switchTab(e) {
    const tab = e.currentTarget.dataset.tab
    this.setData({ tab })
  },

  formatDateToString(date) {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    return `${year}年${month}月${day}日`
  },

  prevMonth() {
    const { year, month } = this.data
    let newYear = year
    let newMonth = month - 1
    
    if (newMonth < 1) {
      newMonth = 12
      newYear--
    }
    
    this.setData({
      year: newYear,
      month: newMonth
    })
    this.generateCalendar(this.data.selectedDate)
  },

  nextMonth() {
    const { year, month } = this.data
    let newYear = year
    let newMonth = month + 1
    
    if (newMonth > 12) {
      newMonth = 1
      newYear++
    }
    
    this.setData({
      year: newYear,
      month: newMonth
    })
    this.generateCalendar(this.data.selectedDate)
  },

  onMonthChange(e) {
    const date = new Date(e.detail.value)
    this.setData({
      year: date.getFullYear(),
      month: date.getMonth() + 1
    })
    this.generateCalendar(e.detail.value)
  },

  onDateChange(e) {
    const date = e.detail.value
    this.generateCalendar(date)
    
    // 返回到上一页并传递选中的日期
    const pages = getCurrentPages()
    const prevPage = pages[pages.length - 2]
    
    prevPage.setData({
      selectedDateStr: date,
      currentDate: this.formatDateToString(new Date(date)),
      selectedDate: new Date(date)
    }, () => {
      wx.navigateBack()
    })
  }
}) 